﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Data;


/// <summary>
/// Summary description for CarResultGridPDFOutput
/// </summary>
public class CarResultGridPDFOutput
{
    private string _carid;
    private DataTable _dtresult;
    private string _title;
    private string _footer;
    
    private string _customername;
    private string _mobile;
    private string _email;
    private string _gender;
    private string _maritalstatus;

    private string _dob;
    private string _driveyearsg;
    private string _occupation;
    private string _jobnature;

    private string _currentinsurer;
    private string _carmakemodel;
    private string _manyear;
    private string _regyear;
    private string _appncd;
    private string _currentncd;
    private string _anyclaim;
    private string _offpeak;
    private string _ofd;
    private string _adddriver;
    private string _commencementdate;
    private string _quotedate;

    public string CarId
    {
        get { return _carid; }
        set { _carid = value; }
    }

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    public string Footer
    {
        get { return _footer; }
        set { _footer = value; }
    }

    public string CustomerName
    {
        get { return _customername; }
        set { _customername = value; }
    }

    public string CurrentInsurer
    {
        get { return _currentinsurer; }
        set { _currentinsurer = value; }
    }

    public string CarMakeModel
    {
        get { return _carmakemodel; }
        set { _carmakemodel = value; }
    }

    public string Email
    {
        get { return _email; }
        set { _email = value; }
    }

    public string Mobile
    {
        get { return _mobile; }
        set { _mobile = value; }
    }

    public string DOB
    {
        get { return _dob; }
        set { _dob = value; }
    }

    public string Gender
    {
        get { return _gender; }
        set { _gender = value; }
    }

    public string MaritalStatus
    {
        get { return _maritalstatus; }
        set { _maritalstatus = value; }
    }

    public string Occupation
    {
        get { return _occupation; }
        set { _occupation = value; }
    }

    public string JobNature
    {
        get { return _jobnature; }
        set { _jobnature = value; }
    }

    public string DriveYearSg
    {
        get { return _driveyearsg; }
        set { _driveyearsg = value; }
    }

    public string ManfactYear
    {
        get { return _manyear; }
        set { _manyear = value; }
    }

    public string RegYear
    {
        get { return _regyear; }
        set { _regyear = value; }
    }

    public string CurrentNCD
    {
        get { return _currentncd; }
        set { _currentncd = value; }
    }

    public string AppNCD
    {
        get { return _appncd; }
        set { _appncd = value; }
    }

    public string Offpeak
    {
        get { return _offpeak; }
        set { _offpeak = value; }
    }

    public string OFD
    {
        get { return _ofd; }
        set { _ofd = value; }
    }

    public string AddDriver
    {
        get { return _adddriver; }
        set { _adddriver = value; }
    }

    public string AnyClaim
    {
        get { return _anyclaim; }
        set { _anyclaim = value; }
    }

    public string CommnecementDate
    {
        get { return _commencementdate; }
        set { _commencementdate = value; }
    }

    public string QuoteDateTime
    {
        get { return _quotedate; }
        set { _quotedate = value; }
    }

    public DataTable ResultGridTable
    {
        get { return _dtresult; }
        set { _dtresult = value; }
    }

    public CarResultGridPDFOutput()
    {
        //
        // TODO: Add constructor logic here
        //
        _title = "";
        _footer = "";
        _dtresult = null;
    }

    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime(); 

    public string GeneratePDF()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        document.Info.Title = "Created with PDFsharp";

        // Create an empty page
        PdfPage page = document.AddPage();

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

        //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

        // Create a font
        XFont fontHeader = new XFont("Verdana", 10, XFontStyle.Bold);
        XFont font = new XFont("Verdana", 6.5d, XFontStyle.Regular);
        XFont fontFooter = new XFont("Verdana", 6, XFontStyle.Bold);
        XColor _xcolor = new XColor();
        _xcolor.R = 0;
        _xcolor.G = 0;
        _xcolor.B = 0;
        System.Drawing.Rectangle _rect = new System.Drawing.Rectangle(10, 10, 500, 100);
        XPen _pen = new XPen(_xcolor);
        // Draw the text
        //gfx.DrawRectangle(_pen, _rect);
        //gfx.DrawString("Hello, World!", font, XBrushes.Black,
        //  new XRect(15, 15, 15, 15),
        //  XStringFormats.TopLeft);
        int curX = 50;
        int curY = 50;

        gfx.DrawString(_title, fontHeader, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);

        curY += 20;

        gfx.DrawString("Name", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_customername, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Date of Birth", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_dob, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Email", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_email, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Mobile", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_mobile, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Gender", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_gender, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Marital Status", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_maritalstatus, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Occupation", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_occupation, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Job Nature", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_jobnature, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Current Insurer", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_currentinsurer, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        curY += 20;
        gfx.DrawString("Make/Model", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_carmakemodel, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Year of Driving in Singapore", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_driveyearsg, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Year of Manufacture", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_manyear, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Applicable/Renewal NCD", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_appncd, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Year of Registration", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_regyear, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Current/Existing NCD", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_currentncd, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Any Claim in the Last 3 Year", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_anyclaim, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Offence-Free Driving/COM", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_ofd, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Offpeak Vehicle?", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_offpeak, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Add Additional Name Driver", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_adddriver, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 10;
        gfx.DrawString("Cover Commencement Date", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_commencementdate, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        //curY += 10;
        gfx.DrawString("Quote Date/Time", font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_quotedate, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 20;
        if (_dtresult != null && _dtresult.Rows.Count > 0)
        {
            DataTable _dtResultClean = new DataTable("tblResultClean");
            _dtResultClean.Columns.Add("Logo");
            _dtResultClean.Columns.Add("Insurer Name");
            _dtResultClean.Columns.Add("Plan Name");
            _dtResultClean.Columns.Add("Premium");
            //_dtResultClean.Columns.Add("Excess");

            for (int i = 0; i < _dtresult.Rows.Count; i++)
            {
                DataRow _newRows = _dtResultClean.NewRow();
                string _logoUrl = _dtresult.Rows[i][0].ToString();
                _newRows[0] = _logoUrl.Substring(1, _logoUrl.Length - 1);
                _newRows[1] = _dtresult.Rows[i][1];
                _newRows[2] = _dtresult.Rows[i][2];
                _newRows[3] = Convert.ToDouble(_dtresult.Rows[i][7]).ToString("$#,000.00");
                //int _intExcess = Convert.ToInt32(_dtresult.Rows[i][8].ToString());
                //if (_intExcess >= 1000)
                //{
                //    _newRows[4] = Convert.ToDouble(_dtresult.Rows[i][8]).ToString("$#,000");
                //}
                //else
                //{
                //    _newRows[4] = "$" + _intExcess.ToString();
                //}
                _dtResultClean.Rows.Add(_newRows);
            }
            //string[] _arrHeader = { "Name", "Email", "NRIC", "Post Code" };
            int _colCount = _dtResultClean.Columns.Count;
            int _rowCount = _dtResultClean.Rows.Count;

            string[] _arrHeader = new string[_colCount];
            int[] _colWidth = { 60, 80, 120, 60 };
            for (int i = 0; i < _colCount; i++)
            {
                _arrHeader[i] = _dtResultClean.Columns[i].ColumnName;
            }

            string[,] _arrData = new string[_rowCount, _colCount];
            for (int r = 0; r < _rowCount; r++)
            {
                for (int c = 0; c < _colCount; c++)
                {
                    _arrData[r, c] = _dtResultClean.Rows[r][c].ToString();
                }
            }

            DrawTable(gfx, curX, curY + 5, _rowCount, _colCount, 25, _colWidth, _arrHeader, _arrData);

            curY += (25 + (_rowCount * 25)); //Talbe place holder
        }
        else
        {
            curY += 10;
            gfx.DrawString("You got no quotes result.", fontFooter, XBrushes.Red, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        }
        curY += 10; //New line
        gfx.DrawString(_footer, fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 30;

        gfx.DrawString("Warmest Regards,", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 10;
        gfx.DrawImage(XImage.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/logos/gluaygluay.png"), new XRect(curX, curY, 80, 35));
        curY += 30;
        gfx.DrawString("GlauyGluay Team", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 10;

        //gfx.DrawString(DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"), fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft); 
        string _filePath = GetFilePath();
        string filename = _filePath + "Quotes_" + _carid + dateTimeNow.ToString("_yyyyMMddhhmmss") + ".pdf"; //DateTime.Now.ToString("_yyyyMMddhhmmss")
        //document.Save(filename);
        // ...and start a viewer.
        //Response.Redirect(filename);
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        document.Save(filename);
        stream.Close();
        document.Close();
        return filename;
    }

    private string GetFilePath()
    {
        string _filePath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("NonGGFileOutputPath").ToString();
        _filePath += "pdfoutput\\carinsurance\\";
        CE.InputOut.MyIO _myIO = new CE.InputOut.MyIO();
        string _todaydate = dateTimeNow.ToString("yyyyMMMdd"); //DateTime.Now.ToString("yyyyMMMdd");
        if (!_myIO.CheckDirectory(_filePath + _todaydate))
        {
            if (_myIO.CreateDirectory(_filePath, _todaydate))
            {
                _filePath += _todaydate + "\\";
            }
        }
        else
        {
            _filePath += _todaydate + "\\";
        }

        return _filePath;
    }
    private void DrawTable(XGraphics _objG, int x, int y, int rows, int cols, int rowHeight, int[] _arrColsWidth, string[] _arrHeader, string[,] _arrData)
    {
        XColor _xcolor = new XColor();
        _xcolor.R = 0;
        _xcolor.G = 0;
        _xcolor.B = 0;
        //System.Drawing.Rectangle _rect = new System.Drawing.Rectangle(0, 0, 0, 0);
        XRect _rect = new XRect(0, 0, 0, 0);
        _rect.X = x;
        _rect.Y = y;
        _rect.Width = _arrColsWidth[0];
        _rect.Height = rowHeight;
        XPen _pen = new XPen(_xcolor);
        _pen.Width = 0.5d;
        XFont font = new XFont("Verdana", 6, XFontStyle.Regular);
        XFont headerFont = new XFont("Verdana", 6, XFontStyle.Bold);
        for (int h = 0; h < cols; h++)
        {
            _rect.Width = _arrColsWidth[h];
            _objG.DrawRectangle(_pen, _rect);
            _objG.DrawString(_arrHeader[h].ToString(), headerFont, XBrushes.Black, new XRect(_rect.X + 10, _rect.Y + 10, 10, 10), XStringFormats.TopLeft);
            _rect.X += _arrColsWidth[h];
        }

        _rect.Y += rowHeight;
        _rect.X = x;

        for (int i = 0; i < rows; i++)
        {
            // Draw the text
            for (int j = 0; j < cols; j++)
            {
                _rect.Width = _arrColsWidth[j];
                _objG.DrawRectangle(_pen, _rect);
                if (j == 0)
                {
                    _objG.DrawImage(XImage.FromFile(AppDomain.CurrentDomain.BaseDirectory + _arrData[i, j].ToString()), new XRect(_rect.X + 5, _rect.Y + 3, 50, 20));
                }
                else
                {
                    _objG.DrawString(_arrData[i, j].ToString(), font, XBrushes.Black, new XRect(_rect.X + 3, _rect.Y + 3, 10, 10), XStringFormats.TopLeft);
                }
                _rect.X += _arrColsWidth[j];
            }
            _rect.Y += rowHeight;
            _rect.X = x;
        }
    }


}
